package com.haixiaoke.saas.system.framework.web.service;


import com.haixiaoke.saas.common.core.domain.entity.SysRole;
import com.haixiaoke.saas.common.core.domain.entity.SysUser;
import com.haixiaoke.saas.role.domain.Role;
import com.haixiaoke.saas.role.service.IRoleService;
import com.haixiaoke.saas.system.service.ISysMenuService;
import com.haixiaoke.saas.system.service.ISysRoleService;
import com.haixiaoke.saas.user.domain.UserBusiness;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 用户权限处理
 *
 * @author haixiaoke
 */
@Component
public class SysPermissionService {
    @Autowired
    private ISysRoleService roleService;
    @Autowired
    private IRoleService bizRoleService;

    @Autowired
    private ISysMenuService menuService;

    /**
     * 获取角色数据权限
     *
     * @param user 用户信息
     * @return 角色权限信息
     */
    public Set<String> getRolePermission(SysUser user) {
        Set<String> roles = new HashSet<String>();
        // 管理员拥有所有权限
        if (user.isAdmin()) {
            roles.add("admin");
        } else {
            roles.addAll(roleService.selectRolePermissionByUserId(user.getUserId()));
        }
        return roles;
    }

    public Set<String> getRolePermission(Long userId, String tenantId) {
//        Set<String> roles = new HashSet<String>(iBizRoleService.selectRolePermissionByUserId(user.getUserId()));
        // 管理员拥有所有权限
//        if (user.isAdmin()) {
//            roles.add("admin");
//        } else {
//            roles.addAll(iBizRoleService.selectRolePermissionByUserId(user.getUserId()));
//        }
        return bizRoleService.selectRoleKeyByUserIdAndTenantId(userId, tenantId);
    }

    /**
     * 获取菜单数据权限
     *
     * @param user 用户信息
     * @return 菜单权限信息
     */
    public Set<String> getMenuPermission(SysUser user) {
        Set<String> perms = new HashSet<String>();
        // 管理员拥有所有权限
        if (user.isAdmin()) {
            perms.add("*:*:*");
        } else {
            List<SysRole> roles = user.getRoles();
            if (!roles.isEmpty() && roles.size() > 1) {
                // 多角色设置permissions属性，以便数据权限匹配权限
                for (SysRole role : roles) {
                    Set<String> rolePerms = menuService.selectMenuPermsByRoleId(role.getRoleId());
                    role.setPermissions(rolePerms);
                    perms.addAll(rolePerms);
                }
            } else {
                perms.addAll(menuService.selectMenuPermsByUserId(user.getUserId()));
            }
        }
        return perms;
    }

    public Set<String> getMenuPermission(UserBusiness user, String tenantId) {
        Set<String> perms = new HashSet<String>();
        /*BizRole bizRole = user.getRole();
        if (ObjectUtil.isNotEmpty(bizRole)) {
            Set<String> rolePerms = menuService.selectMenuPermsByBizRoleId(bizRole.getRoleId(), tenantId);
            bizRole.setPermissions(rolePerms);
            perms.addAll(rolePerms);
        } else {
            perms.addAll(menuService.selectMenuPermsByUserId(user.getUserId()));
        }*/
        List<Role> bizRoleList = user.getRoles();
        if (!bizRoleList.isEmpty()) {
            for (Role role : bizRoleList) {
                Set<String> rolePerms = menuService.selectMenuPermsByBizRoleId(role.getRoleId(), tenantId);
                role.setPermissions(rolePerms);
                perms.addAll(rolePerms);
            }
        }
        return perms;
    }
}
